
import sys
import database
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel, QPushButton
from PySide6.QtCore import QTimer, QDateTime
from PySide6.QtGui import QIcon

Remind_ui_path = './ui/Remind.ui'
Icon_path = './ui/icon.png'

class RemindWindow(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("任务提醒")
        self.setWindowIcon(QIcon(f"{Icon_path}"))
        self.setup_ui()
        self.check_tasks()

        # 设置定时器，每小时检查一次任务
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.check_tasks)
        self.timer.start(3600000)  # 1小时 = 3600000毫秒

    def setup_ui(self):
        self.layout = QVBoxLayout(self)

        self.label = QLabel("即将到期的任务:")
        self.layout.addWidget(self.label)

        self.task_list = QLabel()
        self.layout.addWidget(self.task_list)

        self.close_button = QPushButton("关闭")
        self.close_button.clicked.connect(self.hide)
        self.layout.addWidget(self.close_button)

        self.setLayout(self.layout)
        self.resize(300, 200)

    def check_tasks(self):
        tasks = database.read_DB()
        current_date = QDateTime.currentDateTime().toString("yyyy-MM-dd")
        urgent_tasks = []

        for task in tasks:
            task_id, title, category, priority, due_date, status = task
            if status == "未完成":
                # 检查任务是否即将到期（距离截止日期还有1天）
                due_date_obj = QDateTime.fromString(due_date, "yyyy-MM-dd")
                current_date_obj = QDateTime.fromString(current_date, "yyyy-MM-dd")
                days_left = current_date_obj.daysTo(due_date_obj)

                if days_left <= 1 and days_left >= 0:
                    urgent_tasks.append(f"{title} (截止日期: {due_date})")

        if urgent_tasks:
            task_text = "\n".join(urgent_tasks)
            self.task_list.setText(task_text)
            self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = RemindWindow()
    window.show()
    sys.exit(app.exec())
